
import ok

xem = ok.FrontPanel()
if (xem.OpenBySerial('') != 0):
    raise RuntimeError, 'Failed to open USB connection.'

PLL = ok.PLL22150()
xem.GetPLL22150Configuration(PLL)
PLL.SetVCOParameters(333,48)
PLL.SetOutputSource(0,2)
PLL.SetOutputEnable(0,2)
#PLL.SetDiv1(1,10)
xem.SetPLL22150Configuration(PLL)

if (xem.ConfigureFPGA(r'../SynthesisDecoderTestbed/DecoderTestbed.bit') != 0):
    raise RuntimeError, 'Failed to upload bit file to fpga.'


import struct

def convert( sequence ):
    
    buf = ''
    for mult, pattern in sequence:
        buf += struct.pack('BB', pattern, mult)
        
    if len(buf) < 1024:
        buf += (1024 - len(buf))*'\x00'
        
    return buf

def sequence():
    s = []
    for i in range(256):
#        s.append((i%256, 15 if i%2 else 0))
        s.append((0, 255))
    for i in range(256):
#        s.append((i%256, 15 if i%2 else 0))
        s.append((15, 0))
    return s

def go(sequence):
    buf = convert(sequence)
    xem.WriteToBlockPipeIn(0x9c, 1024, buf)




#xem.SetWireInValue(0x00, int('10001001',2))
#xem.UpdateWireIns()
